<template>
  <div class="manage">
    <el-dialog :title="operateType === 'add' ? '新增用户' : '更新用户'" :visible.sync="isShow">
      <common-form :formLabel="operateFormLabel" :form="operateForm" ref="form"></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser">+ 新增</el-button>
    </div>
    <common-table :tableData="tableData" :tableLabel="tableLabel" :config="config" @changePage="getList()" @edit="editUser" @del="delUser"></common-table>
  </div>
</template>

<script>
import CommonForm from '../../components/CommonForm'
import CommonTable from '../../components/CommonTable'
import axios from 'axios'
export default {
  components: {
    CommonForm,
    CommonTable
  },
  data() {
    return {
      operateType: 'add',
      isShow: false,
      tableData: [],
      tableLabel: [
        {
          prop: 'username',
          label: '姓名'
        },
        // {
        //   prop: 'userpwd',
        //   label: '密码'
        // },
        {
          prop: 'userrole',
          label: '角色'
        }
      ],
      config: {
        page: 1,
        total: 30,
        loading: false
      },
      operateForm: {
        id: '',
        username: '',
        userpwd: '',
        userrole: ''
      },
      operateFormLabel: [
        {
          model: 'username',
          label: '姓名'
        },
        {
          model: 'userpwd',
          label: '密码'
        },
        {
          model: 'userrole',
          label: '角色',
          type: 'select',
          opts: [
            {
              label: '管理员',
              value: '管理员'
            },
            {
              label: '收银员',
              value: '收银员'
            }
          ]
        }
      ]
    }
  },
  methods: {
    getList() {
      this.config.loading = true
      axios.get('http://127.0.0.1:8003/user').then(res => {
        this.tableData = res.data
        this.config.total = res.data.length
        this.config.loading = false
      })
    },
    addUser() {
      this.operateForm = {}
      this.operateType = 'add'
      this.isShow = true
    },
    editUser(row) {
      this.operateType = 'edit'
      this.isShow = true
      this.operateForm = row
    },
    confirm() {
      if (this.operateType === 'edit') {
        axios.put('http://127.0.0.1:8003/user-edit', this.operateForm).then(res => {
          this.isShow = false
          this.getList()
        })
      } else {
        axios.post('http://127.0.0.1:8003/user-add', this.operateForm).then(res => {
          console.log(res.data)
          this.isShow = false
          this.getList()
        })
      }
    },
    delUser(row) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          debugger
          let id = row.id
          axios.delete('http://127.0.0.1:8003/user-delete', {
              params: {
                id
              }
            }).then(res => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getList()
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/scss/common';
</style>
